package com.zebra.remoting;


import com.zebra.remoting.protocol.RemotingCommand;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class SyncZebraFuture<T extends RemotingCommand> implements Future<T> {

    private T command;
    private long beginTime = System.currentTimeMillis();

    public SyncZebraFuture() {
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        if (command != null) {
            return true;
        }
        return false;
    }

    @Override
    public T get() throws InterruptedException {
        return this.command;
    }

    // 获取响应结果，直到有结果或者超过指定时间就返回。
    @Override
    public T get(long timeout, TimeUnit unit) throws InterruptedException {
        return this.command;
    }

    // 用于设置响应结果，并且做countDown操作，通知请求线程
    public void setResponse(T response) {
        this.command = response;
    }

    public long getBeginTime() {
        return beginTime;
    }
}